package com.graduate.cec.service;

import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.LoveMapper;
import com.graduate.cec.pojo.entity.Love;
import com.graduate.cec.pojo.entity.LoveExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class LoveService {

    @Resource
    private LoveMapper loveMapper;



    /****************        增       ******************************************************************/
    public void insertLove(Love love){

        int i = loveMapper.insertSelective(love);
        log.info("返回的love:{}",love);
        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }












    /****************        删       ******************************************************************/
    public void deleteByKeys(Long id){
        LoveExample loveExample = new LoveExample();
        LoveExample.Criteria criteria = loveExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = loveMapper.deleteByExample(loveExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/


    /****************        查       ******************************************************************/

    public List<Love> getCount(Love love){
        LoveExample loveExample = new LoveExample();
        LoveExample.Criteria criteria = loveExample.createCriteria();
        criteria.andUserIdEqualTo(love.getUserId());
        criteria.andLoveIdEqualTo(love.getLoveId());
        List<Love> loves = loveMapper.selectByExample(loveExample);

        return loves;
    }

}